home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / gle / util / manip / filemenu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-29  |  12.8 KB  |  627 lines

  1. #include "all.h"
  2. #include "color.h"
  3. #include "edt.h"
  4.  
  5. #ifdef __TURBOC__
  6. #define DASHCHAR 249
  7. #include "bios.h"
  8. #include "conio.h"
  9. #include "dir.h"
  10. #else
  11. #define DASHCHAR '.'
  12. #include "vaxconio.h"
  13. #endif
  14.  
  15. #define true (!false)
  16. #define false 0
  17. #define BEGINDEF extern
  18. /* #include "begin.h" */
  19. #define BCOLOR BLUE
  20. #define FCOLOR MAGENTA
  21. #define HBCOLOR h_bcolor
  22. #define HVCOLOR h_fcolor
  23. #define VCOLOR WHITE
  24.  
  25. int h_bcolor;
  26. int h_fcolor;
  27. int getsize(void);
  28. char *tabtospace(char *s);
  29. int printdash(int i);
  30. char *gledir(char *s);
  31. int scr_menuval(void);
  32. int scr_menubg(void);
  33. int scr_menuhi(void);
  34. int scr_grey(void);
  35. int gle_redraw(void);
  36. int text_showerror(void);
  37. int text_save(void);
  38. char *strfile(char *s);
  39.  
  40. int vdelete(int i);
  41. int vinsert(int y, char *s);
  42. int m_ifsend(char *s);
  43. int m_sendline(void);
  44. int minit_extras(void);
  45. int minit_extras(void);
  46. int initmenu(void);
  47. int mystrlen(char *s);
  48. int pick_file(char *d, char *w);
  49. int ncpy(char *d, char *s, int n);
  50. int setvstr(char **d, char *s);
  51. int text_inkey(void);
  52.  
  53. struct menu_struct { int x; int y; char *title; int width; int typ; int typ2;
  54.     char *val; char *help; };
  55. typedef struct menu_struct menutype;
  56. struct pmenu_struct {int ci; menutype *menu; };
  57. typedef struct pmenu_struct pmenutype;
  58. pmenutype pmenu[17];
  59. int setmenu(menutype *m,int x, int y, int typ, int typ2, int width, char *title
  60.     , char *val, char *help);
  61. int do_menu(pmenutype *p);
  62. int token_space(void);
  63. int extractmenu(void);
  64. int add_unrecognized(char *s);
  65. int fillinmenu(int nbegin);
  66. int nunrec;
  67.  
  68. #define  PTXT 0
  69. enum {MEND,MTEXT,MFILE,MTOGON,MTOGOFF,MSUB,MRETURN,MNULL};
  70. enum {SNORM,SROL,SNEXTS,SNEXT,SSTR,STOG,SS,SSIZE,SNOBOX,SAMOVE,SSOFF,SSNULL};
  71. int refresh_menu(struct menu_struct *m);
  72. int menu_hilight(struct menu_struct *m);
  73. int menu_norm(struct menu_struct *m);
  74.  
  75. /* menu's
  76.     topmenu
  77.         title,xtitle,ytitle min max dist on off, ticks dticks
  78.         (2) let d = exp
  79.         Data files
  80.         Dn marker ,color, lwidth, lstyle, err,
  81.     xnamesmenu
  82.     ynamesmenu
  83.     fillmenu
  84.     barmenu
  85.     axismenu's
  86. */
  87. read_default(char *result, char *ques, char *dflt)
  88. {
  89.     char s[200];
  90.     int r;
  91.     strcpy(s,ques); strcat(s," ["); strcat(s,dflt); strcat(s,"] ");
  92. /*    r = read_input(result,s);*/
  93.     if (strlen(result)==0) strcpy(result,dflt);
  94.     return r;
  95. }
  96. extern int changed;
  97. addline(char *s)
  98. {
  99.     changed = true;
  100. }
  101. char *mark_names[] = {
  102.     "dot", "square", "fcircle", "club", "diamond", "triangle", "snake"
  103.     ,"otimes", "odot", ""};
  104.  
  105. int init_menucolor(void);
  106. int m_gstart,m_gend;
  107. /*
  108.  
  109.     Getkey/command
  110.         arrowkeys normal
  111.         left and right arrow if editing
  112.         delete
  113.         insert normal character
  114.         enter, change to new menu
  115.         escape exit.
  116.         F1, help key
  117. */
  118. char mbuff[255];
  119.  
  120. extern int iserr;
  121. int hcx,cx,hcy;    /* HIlighted current x,y */
  122. do_menu(pmenutype *ppmenu)
  123. {
  124.     menutype *cm,*cmi,*si=NULL,*mi;
  125.     int citem,emode=false;
  126.     int c;
  127.     cm = ppmenu->menu;
  128.     citem = ppmenu->ci;
  129.     cmi = cm+citem;
  130.     refresh_menu(cm);
  131.     menu_hilight(cmi);
  132.     for (;;) {
  133.      c = text_inkey();
  134.      if (iserr) {fner_clear(); window(1,1,80,24);}
  135.      switch (c) {
  136.        case eescape: /* ESCAPE */
  137.        case equit: /* control c */
  138.         return true;
  139.        case eleft: /* left */
  140.         if (emode) {
  141.             if (cx>0) cx--;
  142.             gotoxy(hcx+cx,hcy);
  143.         } else {
  144.             for (si=NULL,mi=cm;mi->typ!=0;mi++) {
  145.                 if (mi->y==cmi->y && mi->x < cmi->x)
  146.                 if (si!=NULL) {
  147.                     if (mi->x > si->x)
  148.                         si = mi;
  149.                 } else si = mi;
  150.             }
  151.         }
  152.         break;
  153.        case eright: /* right */
  154.         if (emode) {
  155.             if (cx < strlen(cmi->val)) cx++;
  156.             gotoxy(hcx+cx,hcy);
  157.         } else {
  158.             for (si=NULL,mi=cm;mi->typ!=0;mi++) {
  159.                 if (mi->y==cmi->y && mi->x > cmi->x)
  160.                 if (si!=NULL) {
  161.                     if (mi->x < si->x)
  162.                         si = mi;
  163.                 } else si = mi;
  164.  
  165.             }
  166.         }
  167.         break;
  168.        case eup: /* arrow up */
  169.         emode = false;
  170.         for (si=NULL,mi=cm;mi->typ!=0;mi++) {
  171.             if (mi->typ<MNULL) {
  172.              if (si!=NULL) if (mi->y==si->y)
  173.                if (abs(mi->x - cmi->x) <  abs(si->x - cmi->x))
  174.                 si = mi;
  175.              if (mi->y < cmi->y) if (si!=NULL) {
  176.                if (mi->y > si->y) si = mi;
  177.              } else      si = mi;
  178.             }
  179.         }
  180.         break;
  181.        case edown: /* arrow down */
  182.         emode = false;
  183.         for (si=NULL,mi=cm;mi->typ!=0;mi++) {
  184.             if (mi->typ<MNULL) {
  185.              if (si!=NULL) if (mi->y==si->y)
  186.                if (abs(mi->x - cmi->x) <  abs(si->x - cmi->x))
  187.                 si = mi;
  188.              if (mi->y > cmi->y) if (si!=NULL) {
  189.                if (mi->y < si->y) si = mi;
  190.              } else  si = mi;
  191.             }
  192.         }
  193.         break;
  194.       case ehelp: /* f1 help */
  195.         do_help(cmi->help);
  196.         refresh_menu(cm);
  197.         menu_hilight(cmi);
  198.         break;
  199.       case ereturn: /* carriage return */
  200.         if (emode==true && cmi->typ==MFILE) {
  201.             emode = false;
  202.             break;
  203.         }
  204.         emode = false;
  205. doreturn:;
  206.         switch (cmi->typ) {
  207.           case MTEXT:
  208. /*            if (emode) emode = false; else emode = true; */
  209.             break;
  210.           case MTOGON:
  211.             cmi->typ = MTOGOFF;
  212.             menu_hilight(cmi);
  213.             break;
  214.           case MTOGOFF:
  215.             cmi->typ = MTOGON;
  216.             menu_hilight(cmi);
  217.             break;
  218.           case MRETURN:
  219.             goto exit_menu;
  220.           case MFILE:
  221.             if (mystrlen(cmi->val)==0) cmi->val = calloc(20,1);
  222.             pick_file(cmi->val,"*.dat");
  223.             refresh_menu(cm);
  224.             menu_hilight(cmi);
  225.             break;
  226.           case MSUB: /* SUB MENU */
  227.             if (cmi->typ2>99) do_menu(&pmenu[cmi->typ2 - 100]);
  228.             refresh_menu(cm);
  229.             menu_hilight(cmi);
  230.         }
  231.  
  232.         for (si=NULL,mi=cm;mi->typ!=0;mi++) {
  233.             if (mi->y==cmi->y && mi->x > cmi->x)
  234.             if (si!=NULL) {
  235.                 if (mi->x < si->x)
  236.                     si = mi;
  237.             } else si = mi;
  238.         }
  239.  
  240.         break;
  241.       case edelete: /* delete */
  242.         emode = true;
  243.         if (cmi->val==NULL) break;
  244.         if (strlen(cmi->val)==0) break;
  245.         if (cx<1) break;
  246.         ncpy(mbuff,cmi->val,cx-1);
  247.         strcat(mbuff,cmi->val + cx);
  248.         setvstr(&cmi->val,mbuff);
  249.         cx--;
  250.         gotoxy(hcx+cx,hcy);
  251.         cputs(cmi->val + cx);
  252.         putch(DASHCHAR);
  253.         gotoxy(hcx+cx,hcy);
  254.         break;
  255.       case edrawit:
  256.         break;
  257.       case eshowerror:
  258.         break;
  259.       case esave: /* save file */
  260.         break;
  261.       default: /* normal key */
  262.         if (c<26  && c!=9) {fner("Key has no affect"); break;}
  263.         if (c>200)  fner("Unimplemented command");
  264.         else {
  265. /*     if (cmi->typ!=MTEXT  && cmi->typ!=MFILE) goto doreturn; */
  266.             emode = true;
  267.             if (cmi->val==NULL) setvstr(&cmi->val,"");
  268.             ncpy(mbuff,cmi->val,cx);
  269.             mbuff[cx] = c; mbuff[cx+1] = 0;
  270.             strcat(mbuff,cmi->val + cx);
  271.             setvstr(&cmi->val,mbuff);
  272.             gotoxy(hcx+cx,hcy);
  273.             cputs(cmi->val + cx);
  274.             cx++;
  275.             gotoxy(hcx+cx,hcy);
  276.         }
  277.         break;
  278.      }
  279.      if (si!=NULL) {
  280.         menu_norm(cmi);
  281.         cmi = si;
  282.         menu_hilight(cmi);
  283.         si = NULL;
  284.      }
  285.     }
  286. exit_menu:;
  287.     /* save current point */
  288.     { int i;
  289.         for (i=0,mi=cm;mi->typ!=0;mi++,i++) {
  290.           if (mi==cmi) ppmenu->ci = i;
  291.         }
  292.     }
  293.     return false; /* normal exit */
  294. }
  295. setvstr(char **d, char *s)
  296. {
  297.     if ((*d) != 0) myfree(*d);
  298.     *d = sdup(s);
  299. }
  300. int mystrlen(char *s)
  301. {
  302.     if (s==NULL) return 0;
  303.     else return strlen(s);
  304. }
  305. menu_hilight(struct menu_struct *mm)
  306. {
  307.     int i,j;
  308.     struct menu_struct *m=mm;
  309.     scr_menuhi();
  310.     hcx = m->x + mystrlen(m->title);
  311.     hcy = m->y;
  312.     gotoxy(hcx,hcy);
  313.     if (m->typ==MTOGON || m->typ==MTOGOFF) {
  314.         if (m->val==NULL) m->val = malloc(4);
  315.         if (m->typ==MTOGON) strcpy(m->val,"ON ");
  316.         if (m->typ==MTOGOFF) strcpy(m->val,"OFF");
  317.     }
  318.     if (m->val==NULL) setvstr(&m->val,"");
  319.     cputs(m->val);
  320.     j = m->width - mystrlen(m->val);
  321.     printdash(j);
  322.     cx = mystrlen(m->val);
  323.     gotoxy(hcx+cx,hcy);
  324. }
  325. printdash(int j)
  326. {
  327.     int i;
  328.     for (i=0;i<j;i++) mbuff[i] = DASHCHAR;
  329.     mbuff[j] = 0;
  330.     if (j>0) cputs(mbuff);
  331. }
  332. menu_norm(struct menu_struct *mm)
  333. {
  334.     int i,j;
  335.     struct menu_struct *m=mm;
  336.     gotoxy(m->x + mystrlen(m->title),m->y);
  337.     if (m->typ==MTOGON  || m->typ==MTOGOFF) {
  338.         if (m->val==NULL) m->val = malloc(4);
  339.         if (m->typ==MTOGON) strcpy(m->val,"ON ");
  340.         if (m->typ==MTOGOFF) strcpy(m->val,"OFF");
  341.     }
  342.     if (m->val != NULL) {
  343.         scr_menuval();
  344.         if (m->width>0) cputs(m->val);
  345.         scr_menubg();
  346.     }
  347.     j = m->width - mystrlen(m->val);
  348.     printdash(j);
  349. }
  350. refresh_menu(struct menu_struct *mm)
  351. {
  352.     int i;
  353.     struct menu_struct *m=mm;
  354.     window(1,1,80,24);
  355.     scr_menubg();
  356.     clrscr();
  357.     for (;m->typ!=0;m++) {
  358.         if (m->title != NULL) if (strlen(m->title)>0) {
  359.             gotoxy(m->x,m->y);
  360.             cputs(m->title);
  361.         }
  362.         menu_norm(m);
  363.     }
  364. }
  365. do_help(char *k1, char *k2)
  366. {
  367.     FILE *hfile;
  368.     int i,yy;
  369.     char hbuff[90];
  370.     char *hfilename="manip.hlp";
  371.     char fk1[20],fk2[20],*os,*si;
  372.     scr_menuval();
  373.     clrscr();
  374.     if (strcmp(k2," ")==0) k2 = "";
  375.     gotoxy(1,1); wprintf("Manip help on topic {%s},  sub topic {%s}",k1,k2);
  376.     scr_menubg();
  377.     hfile = fopen(gledir(hfilename),"rt");
  378.     if (hfile==NULL) wprintf("Unable to open {%s} \n",gledir(hfilename));
  379.     for (;!feof(hfile);) {
  380.         if (fgets(hbuff,90,hfile)!=NULL) {
  381.             if (hbuff[0]=='3') {
  382.                 strtok(hbuff," \n");
  383.                 strcpy(fk1,strtok(NULL," \n"));
  384.                 if (strcmp(fk1,k1)==0 && strlen(k2)==0)
  385.                     goto help_type;
  386.             }
  387.             if (hbuff[0]=='4') {
  388.                 strtok(hbuff," \n");
  389.                 strcpy(fk2,strtok(NULL," \n"));
  390.                 if (strlen(k2)>0)
  391.                 if (strcmp(fk1,k1)==0 &&
  392.                     strcmp(fk2,k2)==0) goto help_type;
  393.             }
  394.         }
  395.     }
  396.     fclose(hfile);
  397.     scr_menuval();
  398.     gotoxy(1,9);
  399.     wprintf("help text not found sorry ??? \n");
  400.     text_inkey();
  401.     return;
  402. help_type:;
  403.     yy = 3;
  404.     for (;!feof(hfile);) {
  405.         if (fgets(hbuff,90,hfile)!=NULL) {
  406.             if (isdigit(hbuff[0])) {
  407.                 goto end_help;
  408.             }
  409.             gotoxy(1,yy++); cputs(tabtospace(hbuff));
  410.         }
  411.         if (yy==22) {
  412.             scr_menuval();
  413.             gotoxy(2,24); wprintf("Press any key for next screen of help");
  414.             text_inkey();  clrscr();
  415.             gotoxy(1,1); wprintf("Graph help on topic {%s},  sub topic {%s}",k1,k2);
  416.             scr_menubg();  yy = 3;
  417.         }
  418.     }
  419. end_help:;
  420.     scr_menuval();
  421.     gotoxy(1,yy+1); wprintf("Press any key to continue");
  422.     text_inkey();
  423.     fclose(hfile);
  424. }
  425. add_unrecognized(char *s)
  426. {
  427.     if (nunrec>6) return;
  428. }
  429. int vunquote(char **s);
  430. vunquote(char **ss)
  431. {
  432.     char *s = *ss;
  433.     if (*s=='\"') {
  434.         s[strlen(s)-1] = 0;
  435.         *ss = sdup(s+1);
  436.         myfree(s);
  437.     }
  438. }
  439.  
  440. int initmenudone;
  441. menutype *m_pnt[20];
  442. menutype *m_end[20];
  443. char *m_val[20];
  444. char m_tog[50];
  445. initmenu()
  446. {
  447.     menutype *mi;
  448.     char *s,kw1[80],kw2[80];
  449.     int i,j,nt=0,k,z,ct,used;
  450.     static int nv;
  451.  
  452.     if (!initmenudone) {
  453.       initmenudone = true;
  454.       for (z=0;z<=10;z++) {
  455.         for (mi=pmenu[z].menu; mi->typ!=0; mi++) {
  456.         if (mi->typ==MTOGON || mi->typ==MTOGOFF) {
  457.             m_tog[nt++] = mi->typ;
  458.         }
  459.         if (mi->val!=NULL) {
  460.             m_val[nv] = sdup(mi->val);
  461.             mi->val = sdup(mi->val);
  462.             m_pnt[nv++] = mi;
  463.         }
  464.         }
  465.       }
  466.     } else {
  467.       for (z=0;z<=10;z++) {
  468.         for (mi=pmenu[z].menu; mi->typ!=0; mi++) {
  469.         if (mi->typ==MTOGON || mi->typ==MTOGOFF) {
  470.             mi->typ = m_tog[nt++];
  471.         } else {
  472.             if (mi->val!=0) {
  473.                 myfree(mi->val);
  474.             }
  475.             mi->val = 0;
  476.         }
  477.         }
  478.       }
  479.       for (z=0;z<nv;z++) {
  480.         mi = m_pnt[z];
  481.         mi->val = sdup(m_val[z]);
  482.       }
  483.     }
  484. }
  485.  
  486. char msend[200];
  487. #define mss msend+strlen(msend)
  488. char cs[80]="xyz";
  489. int gstartend;
  490. /* char msend[200]; */
  491. m_sendline()
  492. {
  493.     msend[0] = 0;
  494.     strcpy(cs,"xyz");
  495. }
  496. m_ifsend(char *s)
  497. {
  498.     if (strcmp(s,cs)!=0) {
  499.       m_sendline();
  500.       sprintf(msend,"\t%s ",s);
  501.       strcpy(cs,s);
  502.     }
  503. }
  504. int g_get_usersize(double *x, double *y);
  505. setmenu(menutype *m,int x, int y, int typ, int typ2, int width, char *title
  506. , char *val, char *help)
  507. {
  508.     m->x = x; m->y = y; m->typ = typ; m->typ2 = typ2; m->width = width;
  509.     m->title = sdup(title);
  510.     if (val!=0) m->val = sdup(val);
  511.     m->help = sdup(help);
  512. }
  513. init_menucolor()
  514. {
  515.         h_bcolor = WHITE; h_fcolor = BLACK;
  516. }
  517. int pick_file(char *result, char *wld)
  518. {
  519.     pmenutype ask;
  520.     char wild[80];
  521.     char buff2[80];
  522.     char *f[80];
  523.     char *swap;
  524.     menutype *m,*mi,*mwild;
  525.     struct ffblk ffblk;
  526.     int done,i,x,y,nf,rval,adddir=false;
  527.  
  528.     strcpy(wild,wld);
  529.     ask.ci = 0;
  530. pick_again:;
  531.     x=0; y=0; nf=0;
  532.     init_menucolor();
  533.     done = findfirst(wild,&ffblk,0);
  534.     for (nf=0;nf<70 && !done;nf++) {
  535.         f[nf] = sdup(ffblk.ff_name);
  536.         done = findnext(&ffblk);
  537.     }
  538. sortagain:;
  539.     swap = NULL;
  540.     for (i=0;i<(nf-1);i++) {
  541.         if (strcmp(f[i],f[i+1])>0) {
  542.             swap = f[i]; f[i] = f[i+1]; f[i+1] = swap;
  543.         }
  544.     }
  545.     if (swap != NULL) goto sortagain;
  546.  
  547.     m = calloc(3+nf,sizeof(*m));
  548.     ask.menu = m;
  549.     ask.ci = 0;
  550.     if (nf==0) ask.ci = 1;
  551.     if (m==0) wprintf("memory allocation error");
  552.     for (mi=m,i=0;i<nf;i++,mi++) {
  553.         setmenu(mi,x*20+1,y+5,MRETURN,0,18,"",f[i],"");
  554.         x++;
  555.         if (x==4) {x=0; y++; }
  556.     }
  557.     setmenu(mi,5,1,MNULL,0,0,"Use arrow keys to select file then press return","","");
  558.     mi++;
  559.     setmenu(mi,5,3,MRETURN,0,30,"Disk/Dir Specification ",wild,"");
  560.     mwild = mi;
  561.     mi++; mi->typ = 0;
  562.     rval = do_menu(&ask);
  563.     strcpy(result,((ask.menu)[ask.ci]).val);
  564.     for (i=0;i<nf;i++) myfree(f[i]);
  565.     myfree(m);
  566.  
  567.     if (strcmp(wild,mwild->val)!=0) {
  568.         strcpy(wild,mwild->val);
  569.         if (strstr(wild,"*")==NULL) {
  570.             if (wild[strlen(wild)-1] == '\\')
  571.                 strcat(wild,"*.gle");
  572.             else
  573.                 strcat(wild,"\\*.gle");
  574.         }
  575.         adddir = true;
  576.         goto pick_again;
  577.     }
  578.  
  579.     if (adddir) {
  580.         *(strfile(wild)) = 0;
  581. #ifdef __TURBOC__
  582.         if (strstr(wild,":")!=NULL) {
  583.             setdisk(toupper(*wild)-'A');
  584.         }
  585.         strcpy(buff2,wild+2); strcpy(wild,buff2);
  586. #endif
  587.         if (chdir(wild)!=0) ; /* {perror("Cannot change dir"); delay(3000);}*/
  588.     }
  589.  
  590. /*    if (adddir) {
  591.         *(strfile(wild)+1) = 0;
  592.         strcpy(result,wild);
  593.         strcat(result,((ask.menu)[ask.ci]).val);
  594.     }
  595. */
  596.     return rval;
  597. }
  598. char *strfile(char *s)
  599. {
  600.     char *e;
  601.     e = s;
  602.     for (;*s!=0;s++) {
  603.         if (*s==']'  ||  *s=='\\' || *s=='/' || *s==':') e = s;
  604.     }
  605.     return e;
  606. }
  607. char *tabtospace(char *s)
  608. {
  609.     static char buf[255];
  610.     char *o = &buf[0];
  611.     int p=0,k,df;
  612.     for (;*s!=0;s++) {
  613.         if (*s==9) {
  614.             df = (p/8)*8+8-p;
  615.             for (k=1;k<=df;k++) {
  616.                 *o++ = ' ';
  617.                 p++;
  618.             }
  619.         } else {
  620.             *o++ = *s;
  621.             p++;
  622.         }
  623.     }
  624.     *o = 0;
  625.     return &buf[0];
  626. }
  627.